home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / exec / makelibrary.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  4KB  |  140 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: makelibrary.c,v 1.4 1996/08/13 13:56:04 digulla Exp $
  4.     $Log: makelibrary.c,v $
  5.     Revision 1.4  1996/08/13 13:56:04  digulla
  6.     Replaced __AROS_LA by __AROS_LHA
  7.     Replaced some __AROS_LH*I by __AROS_LH*
  8.     Sorted and added includes
  9.  
  10.     Revision 1.3  1996/08/01 17:41:14  digulla
  11.     Added standard header for all files
  12.  
  13.     Desc:
  14.     Lang: english
  15. */
  16. #include <exec/execbase.h>
  17. #include <exec/memory.h>
  18. #include <dos/dos.h>
  19. #include <aros/libcall.h>
  20. #include "machine.h"
  21.  
  22. /*****************************************************************************
  23.  
  24.     NAME */
  25.     #include <clib/exec_protos.h>
  26.  
  27.     __AROS_LH5(struct Library *, MakeLibrary,
  28.  
  29. /*  SYNOPSIS */
  30.     __AROS_LHA(APTR,       funcInit,   A0),
  31.     __AROS_LHA(APTR,       structInit, A1),
  32.     __AROS_LHA(ULONG_FUNC, libInit,    A2),
  33.     __AROS_LHA(ULONG,      dataSize,   D0),
  34.     __AROS_LHA(BPTR,       segList,    D1),
  35.  
  36. /*  LOCATION */
  37.     struct ExecBase *, SysBase, 14, Exec)
  38.  
  39. /*  FUNCTION
  40.     Allocates memory for the library, builds it and calls the library's
  41.     init vector. Generally this function is for internal use and for
  42.     use by library programmers that don't want to use the automatic
  43.     initialization procedure.
  44.  
  45.     INPUTS
  46.     funcInit   - Either a pointer to an array of function offsets
  47.              (starts with -1, relative to funcInit) or to an array
  48.              of absolute function pointers.
  49.     structInit - Pointer to a InitStruct() data region or NULL.
  50.     libInit    - The library's init vector or NULL.
  51.              The init vector is called with the library address (D0),
  52.              the segList (A0) and ExecBase (A6) as arguments.
  53.              If the init fails the init code has to free the base memory
  54.              and return NULL (the library address for success).
  55.     dataSize   - Size of the library structure including system structures.
  56.              Must be at least sizeof(struct Library).
  57.     segList    - BCPL pointer to the library segments. Used to free the
  58.              library later.
  59.  
  60.     RESULT
  61.     The library base address or NULL.
  62.  
  63.     NOTES
  64.     The library base is always aligned to the maximum of sizeof(LONG)
  65.     and double alignment restrictions.
  66.  
  67.     EXAMPLE
  68.  
  69.     BUGS
  70.  
  71.     SEE ALSO
  72.     AddLibrary(), RemLibrary(), MakeFunctions(), InitStruct(), SumLibrary().
  73.  
  74.     INTERNALS
  75.  
  76.     HISTORY
  77.  
  78. ******************************************************************************/
  79. {
  80.     __AROS_FUNC_INIT
  81.  
  82.     struct Library *library;
  83.     ULONG negsize=0;
  84.  
  85.     /* Calculate the jumptable's size */
  86.     if(*(WORD *)funcInit==-1)
  87.     {
  88.     /* Count offsets */
  89.     WORD *fp=(WORD *)funcInit+1;
  90.     while(*fp++!=-1)
  91.         negsize+=sizeof(struct JumpVec);
  92.     }
  93.     else
  94.     {
  95.     /* Count function pointers */
  96.     void **fp=(void **)funcInit;
  97.     while(*fp++!=(void *)-1)
  98.         negsize+=sizeof(struct JumpVec);
  99.     }
  100.  
  101.     /* Align library base */
  102.     negsize=(negsize+(LIBALIGN-1))&~(LIBALIGN-1);
  103.  
  104.     /* Allocate memory */
  105.     library=(struct Library *)AllocMem(dataSize+negsize,MEMF_PUBLIC|MEMF_CLEAR);
  106.  
  107.     /* And initilize the library */
  108.     if(library!=NULL)
  109.     {
  110.     /* Get real library base */
  111.     library=(struct Library *)((char *)library+negsize);
  112.  
  113.     /* Build jumptable */
  114.     if(*(WORD *)funcInit==-1)
  115.         /* offsets */
  116.         MakeFunctions(library,(WORD *)funcInit+1,(WORD *)funcInit+1);
  117.     else
  118.         /* function pointers */
  119.         MakeFunctions(library,funcInit,NULL);
  120.  
  121.     /* Write sizes */
  122.     library->lib_NegSize=negsize;
  123.     library->lib_PosSize=dataSize;
  124.  
  125.     /* Create structure */
  126.     if(structInit!=NULL)
  127.         InitStruct(structInit,library,0);
  128.  
  129.     /* Call init vector */
  130.     if(libInit!=NULL)
  131.         library=__AROS_ABS_CALL3(struct Library *,libInit,
  132.                      library,D0,segList,A0,SysBase,A6);
  133.     }
  134.     /* All done */
  135.     return library;
  136.  
  137.     __AROS_FUNC_EXIT
  138. } /* MakeLibrary */
  139.  
  140.